package src.adiel.cameraApp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

import src.adiel.bgu.ButtonListener;
import src.adiel.bgu.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

public class cameraActivity extends Activity implements OnClickListener{

	//Setup the FrameLayout with the Camera Preview Screen
	CameraSurfaceView _cameraSurfaceView;
	private int _counter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this._counter = 0;
		
		//TODO:This order is correct?
		this.setContentView(R.layout.camera_preview);
		this._cameraSurfaceView = new CameraSurfaceView(this);
		FrameLayout preview = (FrameLayout) this.findViewById(R.id.preview); 
		preview.addView(this._cameraSurfaceView);
		
		android.widget.Button tButton = (Button) this.findViewById(R.id.buttonClick);
		tButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		
		Camera camera = this._cameraSurfaceView.getCamera();
		camera.takePicture(null, null, new HandlePictureStorage());
	}
	
	private class HandlePictureStorage implements PictureCallback
	{

		@Override
		public void onPictureTaken(byte[] picture, Camera camera) 
		{
			//The picture can be stored or do something else with the data
			//in this callback such sharing with friends, upload to a Cloud component etc
			
			//This is invoked when picture is taken and the data needs to be processed
			Bitmap bmp=BitmapFactory.decodeByteArray(picture,0,picture.length); 
			Mat raw = new Mat(1, picture.length, CvType.CV_8SC1); 
			raw.put(0,0, picture); 
//			double[]temp;
//			for (int i = 0; i < raw.rows(); i++) {
//				for (int j = 0; j < raw.cols(); j++) {
//					temp = raw.get(i, j);
//					System.out.println("" + temp.toString());
//				}
//			}
			//Log.i(TAG,"raw: " + raw.rows() + " x " + raw.cols()); 
			//Mat colorMat = Highgui.imdecode(raw, 1); 
			//Mat grayMat = Highgui.imdecode(raw, 0); 
			Mat rgba = new Mat(); 
			Imgproc.cvtColor(raw, rgba, Imgproc.COLOR_GRAY2BGRA, 4); 
			Bitmap tOutBitmap = Bitmap.createBitmap(rgba.cols(), rgba.rows(), Bitmap.Config.ARGB_8888); 			
			System.out.println("Picture successfully taken: "+picture);
			//Houston we have a problem
			Utils.matToBitmap(rgba, tOutBitmap);
			//Bitmap tOutBitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
			savePicture(tOutBitmap);
			setContentView(R.layout.camera_preview);
		}
	}
	
	private boolean savePicture(Bitmap bitmap){
		
		
		int tTestSizeH = bitmap.getHeight();
		int tTestSizeW = bitmap.getWidth();
		boolean tAns = true;
		String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
		OutputStream outStream = null;
		File file = new File(extStorageDirectory, this._counter + ".png");
		try {
			outStream = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
			outStream.flush();
			outStream.close();

			Toast.makeText(cameraActivity.this, "Saved", Toast.LENGTH_LONG).show();
			this._counter = this._counter + 1;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			tAns = false;
			e.printStackTrace();
			Toast.makeText(cameraActivity.this, e.toString(), Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			tAns = false;
			e.printStackTrace();
			Toast.makeText(cameraActivity.this, e.toString(), Toast.LENGTH_LONG).show();
		}
		return tAns;
	}
	
	
}
